package com.example.admin.goodnews.fragment.ZaiXianYinYue;

import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.util.ArrayMap;
import android.support.v7.widget.AppCompatSpinner;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;

import com.example.admin.goodnews.R;
import com.example.admin.goodnews.activity.SecondaryPageActivity;
import com.example.admin.goodnews.adapter.PaiHangBangGeDanAdapter;
import com.example.admin.goodnews.entity.PaiHangBangGeDanDataEntity;
import com.example.admin.goodnews.entity.PaiHangBangLeiXingDataEntity;
import com.example.admin.goodnews.interfaces.RecyclerViewItemClickListener;
import com.example.admin.goodnews.interfaces.RecyclerViewItemLongClickListener;
import com.example.admin.goodnews.utils.GetCurrentTimeUtil;
import com.example.admin.goodnews.utils.HttpUtils;
import com.example.admin.goodnews.utils.NetWorkDataUrl;
import com.google.gson.Gson;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 排行榜类型页面
 * Created by admin on 2016/10/18.
 */

public class PaiHangBangLeiXingFragment extends Fragment implements RecyclerViewItemClickListener, RecyclerViewItemLongClickListener {
    private AppCompatSpinner appcompatspinner;// 下拉菜单
    private RecyclerView recyclerview_paihangbangleixing;
    private String time = GetCurrentTimeUtil.getCurrentTime();// 获取当前系统时间
    private LinearLayoutManager linearLayoutManager;// 布局管理器
    private PaiHangBangLeiXingDataEntity paiHangBangLeiXingDataEntity;// 排行榜类型实体类
    private PaiHangBangGeDanDataEntity paiHangBangGeDanDataEntity;// 排行榜歌单实体类
    private PaiHangBangGeDanAdapter paiHangBangGeDanAdapter;// 排行榜歌单适配器
    private Map<String, String> types;
    private List<String> typenames;
    private String ids;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = LayoutInflater.from(getActivity()).inflate(R.layout.fragment_paihangbangleixing, null);
        appcompatspinner = (AppCompatSpinner) view.findViewById(R.id.appcompatspinner);
        recyclerview_paihangbangleixing = (RecyclerView) view.findViewById(R.id.recyclerview_paihangbangleixing);
        linearLayoutManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        recyclerview_paihangbangleixing.setLayoutManager(linearLayoutManager);// 设置布局管理器
        setType();
        // getData();
        return view;
    }

    @Override
    public void onActivityCreated(Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }

    public static PaiHangBangLeiXingFragment newInstance() {
        PaiHangBangLeiXingFragment paiHangBangLeiXingFragment = new PaiHangBangLeiXingFragment();
        return paiHangBangLeiXingFragment;
    }

    // 获取歌曲分类
    private void setType() {
        String param = NetWorkDataUrl.PAIHANGBANGLEIXING_PARAM1 + time + NetWorkDataUrl.PAIHANGBANGLEIXING_PARAM2;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.PAIHANGBANGLEIXING_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    paiHangBangLeiXingDataEntity = gson.fromJson(result, PaiHangBangLeiXingDataEntity.class);
                    if (paiHangBangLeiXingDataEntity.getShowapi_res_body().getRankList().size() > 0) {
                        handler.sendEmptyMessage(1);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    // 请求详细的歌曲分类内容
    private void getData() {
        String param = NetWorkDataUrl.GEQUFENLEI_PARAM1 + ids + NetWorkDataUrl.GEQUFENLEI_PARAM2 + time + NetWorkDataUrl.GEQUFENLEI_PARAM3;
        try {
            HttpUtils.doPostAsyn(NetWorkDataUrl.GEQUFENLEI_URL, param, new HttpUtils.CallBack() {
                @Override
                public void onRequestComplete(String result) {
                    Gson gson = new Gson();
                    paiHangBangGeDanDataEntity = gson.fromJson(result, PaiHangBangGeDanDataEntity.class);
                    if (paiHangBangGeDanDataEntity.getShowapi_res_body().getMusicList().size() > 0) {
                        handler.sendEmptyMessage(2);
                    } else {
                        handler.sendEmptyMessage(0);
                    }
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private Handler handler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 0:
                    break;
                case 1:
                    typenames = new ArrayList<String>();
                    types = new ArrayMap<String, String>();
                    for (int i = 0; i < paiHangBangLeiXingDataEntity.getShowapi_res_body().getRankList().size(); i++) {
                        typenames.add(paiHangBangLeiXingDataEntity.getShowapi_res_body().getRankList().get(i).getName());
                        types.put(paiHangBangLeiXingDataEntity.getShowapi_res_body().getRankList().get(i).getName(), paiHangBangLeiXingDataEntity.getShowapi_res_body().getRankList().get(i).getId());
                    }
                    ArrayAdapter<String> arrayadapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item, typenames);
//                        arrayadapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
                    appcompatspinner.setAdapter(arrayadapter);
                    appcompatspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
                        @Override
                        public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
                            ids = types.get(typenames.get(position));// 通过下拉菜单中的选项获取对应的ID值
                            getData();
                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> parent) {

                        }
                    });
                    break;
                case 2:
                    paiHangBangGeDanAdapter = new PaiHangBangGeDanAdapter(getActivity(), paiHangBangGeDanDataEntity);
                    recyclerview_paihangbangleixing.setAdapter(paiHangBangGeDanAdapter);
                    paiHangBangGeDanAdapter.setRecyclerViewItemClickListener(PaiHangBangLeiXingFragment.this);
                    paiHangBangGeDanAdapter.setRecyclerViewItemLongClickListener(PaiHangBangLeiXingFragment.this);
                    break;
            }
        }
    };

    @Override
    public void RecyclerViewItemClickListener(int position) {
//        Intent intent = new Intent(getActivity(), SecondaryPageActivity.class);
//        String name = typenames.get(position);
//        String link = types.get(typenames.get(position));
//        intent.putExtra("title", name);
//        intent.putExtra("link", link);
//        startActivity(intent);

        String title = paiHangBangGeDanAdapter.getTitle(position);
        String link = paiHangBangGeDanAdapter.getLink(position);
        Intent intent = new Intent(getActivity(), SecondaryPageActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("link", link);
        startActivity(intent);
    }

    @Override
    public void RecyclerViewItemLongClickListener(int position) {
//        Intent intent = new Intent(getActivity(), SecondaryPageActivity.class);
//        String name = typenames.get(position);
//        String link = types.get(typenames.get(position));
//        intent.putExtra("title", name);
//        intent.putExtra("link", link);
//        startActivity(intent);

        String title = paiHangBangGeDanAdapter.getTitle(position);
        String link = paiHangBangGeDanAdapter.getLink(position);
        Intent intent = new Intent(getActivity(), SecondaryPageActivity.class);
        intent.putExtra("title", title);
        intent.putExtra("link", link);
        startActivity(intent);
    }
}
